
const Service = require('egg').Service;
const jwt = require('jsonwebtoken');
// 跟controller保持一致
class HomeService extends Service {
  async login() {
    const { app } = this.ctx;
    const { username, password } = this.ctx.request.body;
    let data = await app.mysql.query(`select * from users where username=? and password=?`, [username, password]);
    console.log(data, 'data')
    let token = jwt.sign({ ...data[0] }, app.config.keys, { expiresIn: '1h' })
    if (data.length) {
      return { code: 0, msg: 'success', token }
    } else {
      return { code: -1, msg: 'fail' }
    }
  }
}

module.exports = HomeService;